package com.itheima.tlias84.Service.impl;

import com.itheima.tlias84.Po.InsertDeptParam;
import com.itheima.tlias84.Service.DeptService;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.annotation.Log;
import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import com.itheima.tlias84.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<SelectAllDeptDTO> selectAll() {
//        System.out.println(1/0);
        List<SelectAllDeptDTO> dtos = deptMapper.selectAll();
        return dtos;
    }
    @Log
    @Override
    public int insertDept(InsertDeptParam param) {
        Dept dept = new Dept();
        dept.setName(param.getName());
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        int flag = deptMapper.insertDept(dept);
        return flag;
    }
    @Log
    @Override
    public int deleteById(Integer id) {
//        int a=1;
//        if(a==1){
//            throw new ArithmeticException("全部发生1/0异常");
//        }
        return deptMapper.deleteById(id);

    }

    @Override
    public SelectAllDeptDTO selectDeptById(Integer id) {
        return deptMapper.selectDeptById(id);
    }
    @Log
    @Override
    public ResultUtil updateDeptById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        Dept selected = deptMapper.selectDeptByName(dept.getName());
        if(Objects.nonNull(selected) && selected.getId() != dept.getId()){
            return ResultUtil.fail("部门名称重复");
        }
         deptMapper.updateDeptById(dept);
        return ResultUtil.success();
    }
    @Log
    @Override
    public int batchDelete(List<Integer> ids) {
       return deptMapper.batchDelete(ids);
    }
    @Log
    @Override
    public int batchInsert(List<String> depts) {
        return deptMapper.batchInsert(depts);
    }


}

//       //教研部门名称是否重复
//        //根据id以及name查询部门
//        if ("存在"){
//            //重复
//            return ResultUtil.success();
//        }
//批量插入和删除用xml







